const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    trainDate: '',
    bodyMeasurements: [
      {
        name: '体重',
        unit: 'kg',
        value: ''
      },
      {
        name: '体脂率',
        unit: '%',
        value: ''
      },
      {
        name: '骨骼肌',
        unit: 'kg',
        value: ''
      },
      {
        name: '基础代谢',
        unit: 'kcal',
        value: ''
      },
      {
        name: '胸围',
        unit: 'cm',
        value: ''
      },
      {
        name: '腰围',
        unit: 'cm',
        value: ''
      },
      {
        name: '臀围',
        unit: 'cm',
        value: ''
      },
      {
        name: '大腿围',
        unit: 'cm',
        value: ''
      },
      {
        name: '小腿围',
        unit: 'cm',
        value: ''
      },
      {
        name: '臂围',
        unit: 'cm',
        value: ''
      },
      {
        name: '肩宽',
        unit: 'cm',
        value: ''
      },
      {
        name: '静态心率',
        unit: '次/分',
        value: ''
      },
      {
        name: '台阶测试心率',
        unit: '次/分',
        value: ''
      },
      {
        name: '俯卧撑',
        unit: '次/分',
        value: ''
      },
      {
        name: '靠墙静蹲',
        unit: '秒',
        value: ''
      },
      {
        name: '平板支撑',
        unit: '秒',
        value: ''
      },
      {
        name: '卷腹',
        unit: '个',
        value: ''
      },
      {
        name: '站立体前屈',
        unit: 'cm',
        value: ''
      },
      {
        name: '身体质量指数',
        unit: 'kg/cm²',
        value: ''
      }
    ]
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    const trainDate = wx.getStorageSync("trainDate");
    const bodyMeasurementsJson = wx.getStorageSync("bodyMeasurements");
    const myMeasurements = JSON.parse(bodyMeasurementsJson) || [];
    const bodyMeasurements = this.data.bodyMeasurements;
    bodyMeasurements.forEach(function(item){
      myMeasurements.forEach(function(measurement){
        if (measurement.name===item.name){
          item.value = measurement.value;
        }
      });
    });
    this.setData({
      bodyMeasurements: bodyMeasurements,
      trainDate: trainDate
    });
  },
  inputWeight:function(e){
    const value = e.detail.value;
    const bodyMeasurements = this.data.bodyMeasurements;
    bodyMeasurements[0].value = value;
    this.setData({
      bodyMeasurements: bodyMeasurements
    });
  },
  inputTJL: function (e) {
    const value = e.detail.value;
    const bodyMeasurements = this.data.bodyMeasurements;
    bodyMeasurements[1].value = value;
    this.setData({
      bodyMeasurements: bodyMeasurements
    });
  },
  inputGugeji: function (e) {
    const value = e.detail.value;
    const bodyMeasurements = this.data.bodyMeasurements;
    bodyMeasurements[2].value = value;
    this.setData({
      bodyMeasurements: bodyMeasurements
    });
  },
  inputDaixie: function (e) {
    const value = e.detail.value;
    const bodyMeasurements = this.data.bodyMeasurements;
    bodyMeasurements[3].value = value;
    this.setData({
      bodyMeasurements: bodyMeasurements
    });
  },
  inputXwei: function (e) {
    const value = e.detail.value;
    const bodyMeasurements = this.data.bodyMeasurements;
    bodyMeasurements[4].value = value;
    this.setData({
      bodyMeasurements: bodyMeasurements
    });
  },
  inpuYwei: function (e) {
    const value = e.detail.value;
    const bodyMeasurements = this.data.bodyMeasurements;
    bodyMeasurements[5].value = value;
    this.setData({
      bodyMeasurements: bodyMeasurements
    });
  },
  inputTwei: function (e) {
    const value = e.detail.value;
    const bodyMeasurements = this.data.bodyMeasurements;
    bodyMeasurements[6].value = value;
    this.setData({
      bodyMeasurements: bodyMeasurements
    });
  },
  inputDTwei: function (e) {
    const value = e.detail.value;
    const bodyMeasurements = this.data.bodyMeasurements;
    bodyMeasurements[7].value = value;
    this.setData({
      bodyMeasurements: bodyMeasurements
    });
  },
  inputDTwei: function (e) {
    const value = e.detail.value;
    const bodyMeasurements = this.data.bodyMeasurements;
    bodyMeasurements[7].value = value;
    this.setData({
      bodyMeasurements: bodyMeasurements
    });
  },
  inputXTwei: function (e) {
    const value = e.detail.value;
    const bodyMeasurements = this.data.bodyMeasurements;
    bodyMeasurements[8].value = value;
    this.setData({
      bodyMeasurements: bodyMeasurements
    });
  },
  inputBwei: function (e) {
    const value = e.detail.value;
    const bodyMeasurements = this.data.bodyMeasurements;
    bodyMeasurements[9].value = value;
    this.setData({
      bodyMeasurements: bodyMeasurements
    });
  },
  inputJiankuan: function (e) {
    const value = e.detail.value;
    const bodyMeasurements = this.data.bodyMeasurements;
    bodyMeasurements[10].value = value;
    this.setData({
      bodyMeasurements: bodyMeasurements
    });
  },
  inputJTXL: function (e) {
    const value = e.detail.value;
    const bodyMeasurements = this.data.bodyMeasurements;
    bodyMeasurements[11].value = value;
    this.setData({
      bodyMeasurements: bodyMeasurements
    });
  },
  save:function(){
    const inputMeasurements = [];
    const bodyMeasurements = this.data.bodyMeasurements;
    bodyMeasurements.forEach(function(item){
      if (item.value>0){
        inputMeasurements.push(item);
      }
    });
    const url = app.globalData.url;
    const openId = app.globalData.openId;
    const trainDate = this.data.trainDate;
    wx.request({
      url: url + '/train/saveMeasurements',
      data: {
        openId: openId,
        trainDate: trainDate,
        bodyMeasurements: inputMeasurements
      },
      method: 'POST',
      success: function (response) {
        if (response.data.success) {
          //保存 
          wx.navigateBack({
            delta: 1
          });
          wx.showToast({
            title: '保存成功',
            icon: 'none'
          });
        } else {
          wx.showToast({
            title: '保存失败:' + response.data.message,
            icon: 'none'
          })
        }
      }
    })
  }
})